package org.wso2.carbon.identity.samples.entitlement;

/**
 * Created by IntelliJ IDEA.
 * User: pushpalanka
 * Date: 3/23/11
 * Time: 2:54 PM
 * To change this template use File | Settings | File Templates.
 */

import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.core.services.authentication.AuthenticationAdminStub;
import org.wso2.carbon.identity.samples.entitlement.admin.EntitlementPolicyAdminServiceStub;


import java.util.Scanner;


public class EntitlementSampleClient {


    private static String authCookie;
    private static String backendServerURL;
    private static ConfigurationContext configCtx;



    public static void main(String[] args) throws Exception {
        //URL where the wso2 Identity Server services are running
        backendServerURL = "https://localhost:9443/services/";
        String policyString = null;

        EntitlementSampleClient entitlementSampleClient = new EntitlementSampleClient();
        boolean log = entitlementSampleClient.login();

        if(!log){
            entitlementSampleClient.login();
        }
        //Stub class to utilize the EntitlementPolicyAdminService of the server
        EntitlementPolicyAdminServiceStub oauth = entitlementSampleClient.setEntitlementPolicyAdminServiceParameters();

        EntitlementAdminServiceUtilities adminMethods = new EntitlementAdminServiceUtilities();
        policyString = adminMethods.takePolicy();

       // adminMethods.adminAddPolicy(policyString, oauth);
        adminMethods.adminReadExistingPolicy(oauth);
        adminMethods.adminRemovePolicy(oauth);


    }






private boolean login() {
    String userName = null;
    String password = null;
    String ip = null;
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter Username:");
    userName = scanner.nextLine();
    System.out.println("Enter Password:");
    password = scanner.nextLine();
    System.out.println("Enter your IP:");
    ip = scanner.nextLine();


    //path to keystore and setup truststore properties that contains CA certifcates to trust.
    String path = "C:\\Users\\Pushpalanka\\WSO2Proj\\Installations\\wso2is-3.0.1\\resources\\security\\wso2carbon.jks";
    System.setProperty("javax.net.ssl.trustStore", path);
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    try {
        configCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
    }


    boolean auth = false;
    try {
        if (authenticate(userName, password, ip)) {
//            if(authenticate("admin","admin","127.0.0.0")){
            System.out.println("user logged in.");
            auth = true;
        } else {
            System.out.println("Not allowed to login.");
            auth = false;
        }
    } catch (Exception e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    return auth;
}

    /*
    Setup parameter as relevant to the wso2 Identity Server
     */
    private EntitlementPolicyAdminServiceStub setEntitlementPolicyAdminServiceParameters(){
        String serviceURL = null;
        ServiceClient client = null;
        Options option = null;
        EntitlementPolicyAdminServiceStub oauth = null;

        serviceURL = backendServerURL + "EntitlementPolicyAdminService";

        try{
            oauth = new EntitlementPolicyAdminServiceStub(configCtx, serviceURL);
        }
        catch (org.apache.axis2.AxisFault af){
            af.printStackTrace();
        }
        client = oauth._getServiceClient();
        option = client.getOptions();
        option.setManageSession(true);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, authCookie);


        return oauth;
    }







    /*
    Authenticate user in the Identity Server
     */
    public static boolean authenticate(String userName, String password, String remoteIp) throws Exception {
        String serviceURL = null;
        AuthenticationAdminStub authStub = null;
        boolean authenticate = false;

        serviceURL = backendServerURL + "AuthenticationAdmin";
        authStub = new AuthenticationAdminStub(configCtx, serviceURL);
        authStub._getServiceClient().getOptions().setManageSession(true);
        authenticate = authStub.login(userName, password, remoteIp);
        authCookie = (String) authStub._getServiceClient().getServiceContext().getProperty(
                HTTPConstants.COOKIE_STRING);
        return authenticate;
    }
}
